#define _CRT_SECURE_NO_WARNINGS 1
#include "BinaryTree.h"
#include "Queue.h"



//前遍历
void PreOrder(BTNode* BT)
{
	if (BT == NULL)
	{
		//printf("NULL ");
		return;
	}
	printf("%C ", BT->data);
	PreOrder(BT->left);
	PreOrder(BT->right);
}
//中遍历
void InOrder(BTNode* BT)
{
	if (BT == NULL)
	{
		//printf("NULL ");
		return;
	}
	PreOrder(BT->left);
	printf("%C ", BT->data);
	PreOrder(BT->right);
}
//后遍历
void PostOrder(BTNode* BT)
{
	if (BT == NULL)
	{
		//printf("NULL ");
		return;
	}
	PreOrder(BT->left);
	PreOrder(BT->right);
	printf("%C ", BT->data);
}

BTNode* BuyBTNode(BTData x)
{
	BTNode* newnode = (BTNode*)malloc(sizeof(BTNode));
	assert(newnode);
	newnode->data = x;
	newnode->left = NULL;
	newnode->right = NULL;
	return newnode;
}

void Destroy(BTNode* node)
{
	free(node);
}

int BTSize(BTNode* root)
{
	return root == NULL ? 0 : BTSize(root->left) + BTSize(root->right) + 1;
}

int BinaryTreeLeafSize(BTNode* root)
{
	if (root == NULL)
	{
		return 0;
	}
	if (root->left == NULL && root->right == NULL)
	{
		return 1;
	}
	return BinaryTreeLeafSize(root->left) + BinaryTreeLeafSize(root->right);
}

int BinaryTreeLeafKSize(BTNode* root, int k)
{
	if (root == NULL)
	{
		return 0;
	}
	if (k == 1)
	{
		return 1;
	}
	return BinaryTreeLeafKSize(root->left, k - 1) + BinaryTreeLeafKSize(root->right, k - 1);
}

int BinaryTreeDepth(BTNode* root)
{
	if (root == NULL)
	{
		return 0;
	}
	int LeftDepth = BinaryTreeDepth(root->left);
	int RightDepth = BinaryTreeDepth(root->right);

	if (LeftDepth > RightDepth)
	{
		return 1 + LeftDepth;
	}
	else
	{
		return 1 + RightDepth;
	}
}

BTNode* BinaryTreeFind(BTNode* root, BTData x)
{
	if (root == NULL)
	{
		return NULL;
	}
	if (root->data == x)
	{
		return root;
	}
	BTNode* LeftFind = BinaryTreeFind(root->left, x);
	if (LeftFind != NULL)
	{
		return LeftFind;
	}
	BTNode* RightFind = BinaryTreeFind(root->right, x);
	if (RightFind != NULL)
	{
		return RightFind;
	}
	return NULL;
}


//层序遍历
void BinaryTreeLevelOrder(BTNode* root)
{
	if (root == NULL)
	{
		return;
	}
	Queue q;
	QueueInit(&q);
	QueuePush(&q, root);
	while (QueueSize(&q) > 0)
	{
		BTNode* front = QueueFront(&q);
		QueuePop(&q);
		printf("%C ", front->data);
		if (front->left)
		{
			QueuePush(&q, front->left);
		}
		if (front->right)
		{
			QueuePush(&q, front->right);
		}
	}
	printf("\n");
	QueueDestroy(&q);
}

//利用队列层序遍历判断是否为完全二叉树
bool BinaryTreeComplete(BTNode* root)
{
	if (root == NULL)
	{
		return true;
	}
	Queue q;
	QueueInit(&q);
	QueuePush(&q, root);
    //将地址一个一个放入同时Pop，当pop出一个空地址，则退出循环；
	while (QueueSize(&q) > 0)
	{
		BTNode* front = QueueFront(&q);
		QueuePop(&q);
		if (front)
		{
			QueuePush(&q, front->left);
			QueuePush(&q, front->right);
		}
		else
		{
			break;
		}
	}
    //继续往队列后检查,如果查出非空地址，则不是完全二叉树
	while (QueueSize(&q) > 0)
	{
		BTNode* front = QueueFront(&q);
		QueuePop(&q);
		if (front)
		{
			return false;
		}
	}
	QueueDestroy(&q);
	return true;
}

void BinaryTreeDestroy(BTNode* root)
{
	if (root == NULL)
	{
		return;
	}
	BinaryTreeDestroy(root->left);
	BinaryTreeDestroy(root->right);
	free(root);
}